<template>
  <div class="login-wrapper">
    <el-form
      ref="loginForm"
      :model="form"
      :rules="rules"
      class="login-form"
      label-width="0"
      @keyup.enter.native="submitForm"
    >
      <h2 class="login-title">登录</h2>
      <el-form-item prop="username">
        <el-input
          v-model="form.username"
          placeholder="用户名"
          clearable
          :class="{ 'input-error': errorField === 'username' }"
        />
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="form.password"
          placeholder="密码"
          show-password
          clearable
          type="password"
          :class="{ 'input-error': errorField === 'password' }"
        />
      </el-form-item>
      <el-form-item>
        <el-checkbox v-model="form.remember">记住我</el-checkbox>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          @click="submitForm"
          :loading="loading"
          class="login-btn"
          style="width: 100%"
        >
          登录
        </el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="text" @click="register" class="register-link"
          >没有账号？注册</el-button
        >
      </el-form-item>
      <div v-if="errorMsg" class="error-msg">{{ errorMsg }}</div>
    </el-form>
  </div>
</template>

<script>
import { Login } from "../api/UserLogin";
export default {
  data() {
    return {
      form: {
        username: "",
        password: "",
        remember: false,
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
      loading: false,
      errorMsg: "",
      errorField: "",
    };
  },
  methods: {
    register() {
      this.$router.push("/register");
    },
    submitForm() {
      this.errorMsg = "";
      this.errorField = "";
      this.$refs.loginForm.validate((valid, fields) => {
        if (valid) {
          this.loading = true;
          Login(this.form)
            .then((res) => {
              this.loading = false;
              if (res.code === 200) {
                this.$message.success(res.msg);
                this.$router.push("/home");
              } else {
                this.errorMsg = res.msg || "登录失败";
                this.errorField = "username";
              }
            })
            .catch((err) => {
              this.loading = false;
              this.errorMsg = err?.msg || "网络错误";
            });
        } else {
          this.errorMsg = "请填写完整信息";
          if (fields.username) this.errorField = "username";
          else if (fields.password) this.errorField = "password";
        }
      });
    },
  },
};
</script>

<style scoped>
.login-wrapper {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #fafbfc;
}
.login-form {
  width: 320px;
  padding: 32px 24px 16px 24px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
  border: 1px solid #f0f0f0;
}
.login-title {
  text-align: center;
  font-size: 22px;
  font-weight: 500;
  margin-bottom: 24px;
  color: #222;
  letter-spacing: 2px;
}
.login-btn {
  border-radius: 8px;
  font-weight: 500;
  letter-spacing: 1px;
}
.register-link {
  width: 100%;
  text-align: center;
  color: #888;
  font-size: 14px;
  margin-top: -12px;
}
.error-msg {
  color: #e74c3c;
  text-align: center;
  margin-top: 8px;
  font-size: 14px;
}
.input-error .el-input__inner {
  border-color: #e74c3c !important;
  background: #fff6f6;
}
</style>
    